<?php

namespace App\Http\Controllers\Web;

use App\Models\Banners;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BannersController extends Controller
{
    public function index(Request $request)
    {
        $query = Banners::query()->with('page')->orderBy("page_id")->orderBy("index");
        $page_id = $request->input("page_id");;
        if ($page_id) {
            $query->where('page_id', $page_id);
        }
        return $this->successPaginate($query->paginate(10));
    }

    public function requestData(Request $request)
    {
        $requestData = $this->validate($request, [
            'index' => 'required',
            'page_id' => 'required',
            'link' => 'nullable',
            'img_url' => 'required',
            'title' => "nullable",
            'title_en' => "nullable",
            'platform' => "required",
            'description' => 'nullable',
            'description_en' => "nullable",
        ]);
        return $requestData;
    }

    public function show($id)
    {
        $team = Banners::query()->findOrFail($id);
        return $this->success($team);
    }

    public function store(Request $request)
    {
        $team = new Banners();
        $requestData = $this->requestData($request);
        $team->fill($requestData);
        $team->save();
        return $this->success($requestData);
    }

    public function update(Request $request, $id)
    {
        $team = Banners::query()->findOrFail($id);
        $requestData = $this->requestData($request);
        $team->fill($requestData);
        $team->save();
        return $this->success($requestData);
    }

    public function destroy($id)
    {
        $team = Banners::query()->findOrFail($id);
        $team->delete();
        return $this->success();
    }
}
